package com.dirk.happie.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.dirk.happie.R;
import com.dirk.happie.adapters.FavoritesListAdapter;
import com.dirk.happie.utils.App;
import com.dirk.happie.utils.Keys;

public class MyFavoritesActivity extends Activity {

	private App mApp;
	private FavoritesListAdapter adapter = null;
	ListView lv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_favorites);
		this.mApp = ((App) getApplication());

		createListView();

		footerMenuListeners();
	}

	private void createListView() {
		lv = (ListView) findViewById(R.id.listProducts);
		View footer = getLayoutInflater().inflate(R.layout.favorite_list_footer, null);
		ImageView mapImage = (ImageView) footer.findViewById(R.id.btn_map);
		mapImage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MyFavoritesActivity.this, MapViewActivity.class);

				startActivityForResult(intent, 10);
				
			}
		});
		lv.addFooterView(footer);
		adapter = new FavoritesListAdapter(this, R.layout.favorite_list_item, mApp.getFavorites());
		lv.setFastScrollEnabled(true);
		lv.setScrollingCacheEnabled(false);
		lv.setAdapter(adapter);
	}


	private void footerMenuListeners() {

		View homeButton = findViewById(R.id.home);
		homeButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		View categoriesButton = findViewById(R.id.categories);
		categoriesButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mApp.isLoggedIn()) {
					Intent intent = new Intent(MyFavoritesActivity.this, CategoriesActivity.class);
					intent.putExtra(Keys.KEY_COMPANY_ID, mApp.getCompanyId());
					startActivityForResult(intent, 10);
				} else {
					Toast.makeText(getApplicationContext(), getString(R.string.not_logged_in), Toast.LENGTH_SHORT).show();
				}
			}
		});

		View basketButton = findViewById(R.id.basket);
		basketButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (mApp.isLoggedIn()) {
					Intent intent = new Intent(MyFavoritesActivity.this, BasketActivity.class);
					intent.putExtra(Keys.KEY_COMPANY_ID, mApp.getCompanyId());
					startActivityForResult(intent, 10);
				} else {
					Toast.makeText(getApplicationContext(), getString(R.string.not_logged_in), Toast.LENGTH_SHORT).show();
				}
			}
		});

		View searchButton = findViewById(R.id.search);
		searchButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		setResult(resultCode);
		switch (resultCode) {
		case 100:
			finish();
			break;
			
		case 500:
			adapter = new FavoritesListAdapter(this, R.layout.favorite_list_item, mApp.getFavorites());
			lv.setFastScrollEnabled(true);
			lv.setScrollingCacheEnabled(false);
			lv.setAdapter(adapter);
			break;

		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
//
//	@Override
//	public void onBackPressed() {
//		
//		super.onBackPressed();
//	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

}
